<?php 
namespace app\wx\controller;

use think\Exception;
use think\Controller;
use think\Db;

// 异常处理模块
use app\common\exception\BaseException;
// 引用模型
use app\wx\model\Classic as ClassicModel;
use app\wx\model\Favor as FavorModel;
use think\Console;

class Classic extends Controller {
  // 获取最新的期刊
  public function getLatest() {
    $classic = new ClassicModel();
    $user = $this->request->param('user');
    $data = $classic::order('index', 'desc')->find();

    $favor = new FavorModel();
    $status = $favor::where("user", $user)->where("favor_id", $data->id)->find();
    $count = $favor::where("favor_id", $data->id)->select()->count();

    if($status) {
      $data->like_status = 1;
    }else {
      $data->like_status = 0;
    }
    $data->fav_nums = $count;

    return json([
        "msg"=> "查找成功",
        "code"=> 20000,
        "data"=> $data
    ],200);
  }

  // 新增期刊
  public function addClassic() {
    // 判断是否有id
    if($this->request->param('id')) {
      $pro = ClassicModel::get($this->request->param('id'));
    }else {
      $pro = new ClassicModel();
    }
    $file = request()->file('img');
    // 移动到框架应用根目录/uploads/ 目录下
    $info = $file->move( './uploads');
    if($info){
        // 成功上传后 获取上传信息
        $pro->image =  $info->getSaveName();
        $pro->content = $this->request->param('content');
        // $pro->like_status = $this->request->param('like_status');
        $pro->title = $this->request->param('title');
        $pro->index = $this->request->param('index');
        $pro->url = $this->request->param('url');
        $pro->type = $this->request->param('type');
        $pro->pubdate = $this->request->param('pubdate');
        $data = $pro->save();
        if($data) {
            return json([
                "msg"=> "success",
                "code"=>20000
            ],200);
        }
    }else{
        // 上传失败获取错误信息
        echo $file->getError();
    }
  }

  // 获取某一期的期刊
  public function getClassicByIndex() {
    $classic = new ClassicModel();
    $user = $this->request->param('user');
    $data = $classic::where('index',  $this->request->param('index'))->find();

    $favor = new FavorModel();
    $status = $favor::where("user", $user)->where("favor_id", $data->id)->find();
    $count = $favor::where("favor_id", $data->id)->select()->count();
    if($status) {
      $data->like_status = 1;
    }else {
      $data->like_status = 0;
    }
    $data->fav_nums = $count;

    if($data) {
      return json([
          "msg"=> "success",
          "data"=> $data,
          "code"=>20000
      ],200);
    }
  }

  // 获取点赞信息
  public function getFavor() {
    // 期刊id,用户，type
    $favor = new FavorModel();
    $count = $favor::where('favor_id',  $this->request->param('id'))->select()->count();
    $status = $favor::where('favor_id',  $this->request->param('id'))->where('type', $this->request->param('type'))->where("user", $this->request->param('user'))->find();
    $data = new \stdClass;
    if($status) {
      $data->like_status = 1;
    }else {
      $data->like_status = 0;
    }
    $data->count = $count;
    return json([
        "msg"=> "success",
        "data"=> $data,
        "code"=>20000
    ],200);
    
  }

  // 获取我喜欢的期刊
  public function getMyFavor() {
    $favor = new FavorModel();
    // $data = $favor::where('user',  $this->request->param('user'))->where('type', '<', 400)->select();
    $data =  Db::table('favor')
              ->alias('a')
              ->leftJoin('classic b', 'a.favor_id = b.id')
              ->where('a.user',  $this->request->param('user'))
              ->where('a.type', '<', 400)
              ->select();
    $result = [];
    foreach($data as $item){
      $item['image'] = config('setting.img_prefix').$item['image'];
      $item['fav_nums'] = $favor::where('favor_id', $item['favor_id'])->where('type', '<', 400)->select()->count();
      $result[] = $item;
    }
    
    if($result) {
      return json([
          "msg"=> "success",
          "data"=> $result,
          "code"=>20000
      ],200);
    }
  }

}